Managing SSH keys for Github and Gitlab · GitHub

您所在的位置:网站首页 gitlab permalink Managing SSH keys for Github and Gitlab · GitHub

Managing SSH keys for Github and Gitlab · GitHub

2023-03-31 13:39| 来源: 网络整理| 查看: 265

Managing SSH keys for Github and Gitlab

NOTICE: This guide will help you set ssh keys for GitHub and GitLab. However, this is not going to change your commit user.name or user.email. If you need to change those for specific repositories, just run the following commands while in your repository:

git config user.name "Your Name Here" git config user.email [email protected]

For more info, see this answer. Also, keep in mind this only changes the .git folder inside your repository which never gets added/committed/pushed/uploaded.

I recently had to manage two ssh keys (one for Github and one for Gitlab). I did some research to find the best solution. I am justing putting the pieces together here.

The first question you can ask yourself is can you have the same ssh key for both Github and Gitlab? The answer is yes but it is not advisable.

The best answer is that you should set one ssh key for Github and another one for Gitlab. The first thing to do is install Git if you haven't. Next, you should check for existing ssh-keys on your system:

Open Git Bash. Enter ls -al ~/.ssh to see if existing SSH keys are present: $ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following: id_rsa.pub id_ecdsa.pub id_ed25519.pub

If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key:

Open Git Bash. Paste the text below, substituting in your GitHub email address. $ ssh-keygen -f ~/.ssh/id_ed25519_hub -t ed25519 -C "[email protected]"

Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:

$ ssh-keygen -f ~/.ssh/id_rsa_hub -t rsa -b 4096 -C "[email protected]"

This creates a new ssh key, using the provided email as a label.

> Generating public/private ed25519 key pair. Add your SSH key to the ssh-agent Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually: # start the ssh-agent in the background $ eval `ssh-agent -s` > Agent pid 59566 Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_hub in the command with the name of your private key file. $ ssh-add ~/.ssh/id_ed25519_hub Add the SSH key to your GitHub account Open Git Bash. Copy the SSH public key to your clipboard.

If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace.

$ clip Agent pid 59566 Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_lab in the command with the name of your private key file. $ ssh-add ~/.ssh/id_ed25519_lab Add the public SSH key to your GitLab account

To use SSH with GitLab, copy your public key to your GitLab account.

Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:

Git Bash on Windows:

cat ~/.ssh/id_ed25519_lab.pub | clip Sign in to GitLab. In the top right corner, select your avatar. Select Settings. From the left sidebar, select SSH Keys. In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with ssh-ed25519 or ssh-rsa, and may end with a comment. In the Title text box, type a description, like Work Laptop or Home Workstation. Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.) The expiration date is informational only, and does not prevent you from using the key. However, administrators can view expiration dates and use them for guidance when deleting keys. Select Add key. How to set up an SSH config-file for beginners

Generally, in Windows machine, the SSH config file stored in the following location: /c/Users/PC_USER_NAME/.ssh/

Just follow the steps in below (if you're using the Git Bash):

Go to the .ssh directory /c/Users/PC_USER_NAME/.ssh/, click right mouse button and choose "Git Bash Here". Create a file named "config" with the following command: $ touch config Now open the config file with the command: $ nano config Now write the following lines inside the config file:

Let's assume you've created two files named id_ed25519_hub for Github and id_ed25519_lab for GitLab.

# GITHUB Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_ed25519_hub # GITLAB Host gitlab.com HostName gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_ed25519_lab How to edit files in a terminal with nano?

How can I save the file?

F3 will let you save without exiting. Otherwise, Ctrl + X will prompt you if you've made changes. Press Y when it asks, and Enter to confirm the filename.

How can I quit the editor without saving the changes?

Ctrl + X, then N when it asks if you want to save.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3